<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目编码" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['project:project:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:project:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编码" align="center" prop="projectCode" />
      <el-table-column label="项目全称" align="center" prop="projectFullName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePaymentItem(scope.row)"
            v-hasPermi="['project:project:query']"
          >支付项目</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReport(scope.row)"
            v-hasPermi="['system:report:list']"
          >项目报表</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, scope.row)" v-hasPermi="['project:project:query']">
            <span class="el-dropdown-link">
              <i class="el-icon-d-arrow-right el-icon--right"></i>更多
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleSupervisorBoq" icon="el-icon-circle-check"
                                v-hasPermi="['project:project:query']">监理清单</el-dropdown-item>
              <el-dropdown-item command="handleSupervisorItems" icon="el-icon-circle-check"
                                v-hasPermi="['project:project:query']">监理支付项目</el-dropdown-item>
              <el-dropdown-item command="handleAgencyBoq" icon="el-icon-circle-check"
                                v-hasPermi="['project:project:query']">实验室清单</el-dropdown-item>
              <el-dropdown-item command="handleAgencyItems" icon="el-icon-circle-check"
                                v-hasPermi="['project:project:query']">实验室支付项目</el-dropdown-item>
              <el-dropdown-item command="handleProfileBoq" icon="el-icon-circle-check"
                                v-hasPermi="['project:project:query']">形象清单</el-dropdown-item>
            </el-dropdown-menu>

          </el-dropdown>

        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="路线名称" align="center" prop="routeName" />
      <el-table-column label="路线简称" align="center" prop="routeShortName" />


    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改建设项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目编码" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编码" />
        </el-form-item>
        <el-form-item label="项目全称" prop="projectFullName">
          <el-input v-model="form.projectFullName" placeholder="请输入项目全称" />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" style="width: 100%" />
        </el-form-item>
        <el-form-item label="省份" prop="province">
          <el-select v-model="form.province" placeholder="请选择" style="width: 100%">
            <el-option
              v-for="item in provinceList.data"
              :key="item.name"
              :value="item.name"
              :label="item.name">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="路线名称" prop="routeName">
          <el-input v-model="form.routeName" placeholder="请输入路线名称" />
        </el-form-item>
        <el-form-item label="路线简称" prop="routeShortName">
          <el-input v-model="form.routeShortName" placeholder="请输入路线简称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import provinceJson from '@/data/province.json'
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/project/project";

export default {
  name: "Project",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 建设项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      provinceList: provinceJson,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: null,
        projectName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectCode: [
          {pattern: /^[A-Za-z0-9]+$/, message: "项目编码格式不正确", trigger: "blur"},
          {required: true, message: "项目编码不能为空", trigger: "blur" }
        ],
        projectFullName: [
          { required: true, message: "项目全称不能为空", trigger: "blur" },
          { max: 128, message: '项目全称最大长度为128个字符', trigger: 'blur'}
        ],
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
          { max: 50, message: '项目名称最大长度为50个字符', trigger: 'blur'}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询建设项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectCode: null,
        projectName: null,
        orderNum: null,
        province: null,
        routeName: null,
        routeShortName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加建设项目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const projectId = row.projectId || this.ids
      getProject(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改建设项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids;
      this.$modal.confirm('确认删除选中的数据项？').then(function() {
        return delProject(projectIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 工程支付项目按钮操作 */
    handlePaymentItem(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/payment/index/' + row.projectId, query: { projectId: projectId} })
    },
    /** 项目报表按钮操作 */
    handleReport(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/report/index/' + row.projectId, query: { projectId: projectId} })
    },
    /** 监理清单按钮操作 */
    handleSupervisorBoq(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/supervisor/boq/' + row.projectId, query: { projectId: projectId} })
    },
    /** 监理支付项目按钮操作 */
    handleSupervisorItems(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/supervisor/items/' + row.projectId, query: { projectId: projectId} })
    },
    /** 代建清单按钮操作 */
    handleAgencyBoq(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/agency/boq/' + row.projectId, query: { projectId: projectId} })
    },
    /** 代建支付项目按钮操作 */
    handleAgencyItems(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/agency/items/' + row.projectId, query: { projectId: projectId} })
    },
    /** 形象清单按钮操作 */
    handleProfileBoq(row) {
      const projectId = row.projectId;
      this.$router.push({ path: '/project/profile/boq/' + row.projectId, query: { projectId: projectId} })
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleSupervisorBoq":
          this.handleSupervisorBoq(row);
          break;
        case "handleSupervisorItems":
          this.handleSupervisorItems(row);
          break;
        case "handleAgencyBoq":
          this.handleAgencyBoq(row);
          break;
        case "handleAgencyItems":
          this.handleAgencyItems(row);
          break;
        case "handleProfileBoq":
          this.handleProfileBoq(row);
          break;
        default:
          break;
      }
    },
  }
};
</script>
